package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.Sku;
import com.example.entity.SkuVo;
import com.example.vo.SkusAttrVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
* @author 12057
* @description 针对表【sku(sku信息)】的数据库操作Mapper
* @createDate 2024-05-13 13:32:24
* @Entity com.example.entity.Sku
*/
@Mapper
public interface SkuMapper extends BaseMapper<Sku> {

    @Select("select * from sku where spu_id = #{spuId}")
    List<SkuVo> getSkusBySpuId(@Param("spuId") Integer spuId);

    @Select("select * from admin_sku_exam where spu_id = #{spuId}")
    List<SkuVo> getExamSkusBySpuId(@Param("spuId") Integer spuId);

    @Select("select * from sku where spu_id = #{spuId}")
    List<Sku> getSkuBySpuId(@Param("spuId") Long spuId);

    @Select("select * from sku where id = #{skuId}")
    SkuVo getSkuBySkuId(@Param("skuId") Integer skuId);

    List<SkusAttrVo> getAllSkuSaleAttrBySkuIds(List<Long> skuIds);

    List<SkusAttrVo> getExamAllSkuSaleAttrBySkuIds(List<Long> skuIds);

    @Select("select MIN(price) from sku where spu_id = #{spuId}")
    BigDecimal getMinPrice(@Param("spuId") Integer spuId);

    @Select("select default_image from sku where spu_id = #{spuId} ORDER BY price DESC LIMIT 1")
    String getDefaultImageBySpuId(@Param("spuId") Integer spuId);

    @Select("select url from sku_images where sku_id = #{skuId} order by sort asc")
    List<String> getSkuImagesById(@Param("skuId") Long skuId);

    @Select("select url from admin_sku_images_exam where sku_id = #{skuId} order by sort asc")
    List<String> getExamSkuImagesById(@Param("skuId") Long skuId);

    List<String> getAllSkusImagesById(List<Long> skuIds);

    List<String> getExamAllSkusImagesById(List<Long> skuIds);

    @Select("SELECT id FROM sku WHERE spu_id = #{spuId}")
    List<Long> findSkuIdBySpuId(Long spuId);

    @Update("UPDATE sku SET stock = stock - #{skuQuantity}, sell = sell + #{skuQuantity} WHERE id = #{skuId} AND stock >= #{skuQuantity}")
    int lockStock(@Param("skuId") Long skuId, @Param("skuQuantity") Integer skuQuantity);


    @Update("UPDATE sku SET stock = stock + #{skuQuantity}, sell = sell - #{skuQuantity}  WHERE id = #{skuId}")
    int unlockStock(@Param("skuId") Long skuId, @Param("skuQuantity") Integer skuQuantity);

    @Update("update sku set stock = #{stock} where id = #{skuId}")
    int updateStock(@Param("skuId") Long skuId, @Param("stock") Long stock);
}




